package cn.bonoon.kernel.web.controllers;

import javax.servlet.http.HttpServletRequest;

import cn.bonoon.kernel.events.PanelEvent;
import cn.bonoon.kernel.menus.ChannelInfo;
import cn.bonoon.kernel.menus.IModuleInfo;
import cn.bonoon.kernel.menus.ItemInfo;
import cn.bonoon.kernel.menus.ModuleManager;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.security.PrivilegeChecker;
import cn.bonoon.kernel.support.services.UserService;
import cn.bonoon.kernel.web.models.LayoutModel;

public class StandardLayoutParser extends StandardPanelParser {
	
	public StandardLayoutParser(ModuleManager moduleManager, UserService userService) {
		super(moduleManager, userService);
	}
	
	@Override
	public PanelEvent parse(String mid, HttpServletRequest request, LogonUser user) throws Exception {
		LayoutModel model = new LayoutModel(mid, request, moduleManager.getEndpoint());
		ItemInfo item = moduleManager.find(model);
		PanelEvent event = accessCheck(model, item, user);
		PrivilegeChecker privilege = event.getPrivilege();
		IModuleInfo module = item.getModule();
		ChannelInfo channel = item.getChannel();
		
		model.initLayout(item.navigation(), module);
		//频道就不允许另外重新生成，必须是定义文件里定义的
		model.buildChannel(module, user, privilege, channel.getId());
		
		//这里可以使用外部的或自定义的菜单来实现
		buildMenu(model, channel, privilege, item.getId());
		return event;
	}

	//生成菜单，如果需要使用自定义的菜单，子类可以重写这个方法来实现
	protected void buildMenu(LayoutModel model, ChannelInfo channel, PrivilegeChecker privilege, String selected){
		model.buildMenu(channel, privilege, selected);
	}
}
